package com.yanqu.road.server.manger.union.listener;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.seacraft.pirateriot.config.SeacraftPirateRiotConfig;
import com.yanqu.road.entity.enums.activity.eActivityCookBoyType;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.task.args.CommonActivityUnionRiseRankArgs;
import com.yanqu.road.entity.task.args.RelationActivityUnionRiseRankArgs;
import com.yanqu.road.entity.task.args.UnionRiseRankArgs;
import com.yanqu.road.entity.task.args.union.UnionRechargeUserArgs;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.ICrossUnionSyncListener;
import com.yanqu.road.server.manger.activity.*;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityMgr;
import com.yanqu.road.server.manger.activity.orewar.OreWarActivityRankMgr;
import com.yanqu.road.server.manger.activity.seacraft.SeacraftPirateRiotMgr;
import com.yanqu.road.server.manger.activity.tonbing.TonBingMgr;
import com.yanqu.road.server.manger.activity.tonbingchannel.ChannelTonBingMgr;
import com.yanqu.road.server.manger.activity.tongbing.TongBingMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;

import java.math.BigInteger;
import java.util.List;

public class UnionActivityRemoveConditionDataListener implements ICrossUnionSyncListener {

    @Override
    public void notifySync(int syncType, long playerId, CrossUnionInfo unionInfo, List<CrossUnionMember> memberList) {

        if(syncType == eUnionSyncType.DeleteUnion.getValue() || syncType == eUnionSyncType.QuitUnion.getValue() ||
                syncType == eUnionSyncType.BeKick.getValue()){

            for (CrossUnionMember member : memberList) {
                if(member.getServerId() != GameServer.getInstance().getServerId()){
                    continue;
                }
                removeMemberActivityConditionData(member.getUserId(), unionInfo.getUnionUid());
            }

        }

    }


    private static void removeMemberActivityConditionData(long userId, String unionUid) {
        removeMemberCommonActivityConditionData(userId, unionUid);
        removeMemberOreWarActivityConditionData(userId, unionUid);
        removeMemberDaFuWengActivityConditionData(userId, unionUid);
        removeMemberBowArtActivityConditionData(userId, unionUid);
        removeMemberGhostActivityConditionData(userId, unionUid);
        TongBingMgr.unionChange(unionUid);
        TonBingMgr.unionChange(unionUid);
        ChannelTonBingMgr.unionChange(unionUid);
        removeMemberDragonBoatRaceActivityConditionData(userId, unionUid);
        removeMemberRelationActivityConditionData(userId, unionUid);
        removeMemberSeacraftPirateRiotChange(userId, unionUid);

        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if (null != player) {
            player.notifyListener(eGamePlayerEventType.UnionExpRiseRank.getValue(), new UnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_REMOVE, BigInteger.ZERO, unionUid));
            player.notifyListener(eGamePlayerEventType.UnionTradeWarScoreRiseRank.getValue(), new UnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_REMOVE, BigInteger.ZERO, unionUid));
            player.notifyListener(eGamePlayerEventType.UnionIntimacyRiseRank.getValue(), new UnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_REMOVE, BigInteger.ZERO, unionUid));
            player.notifyListener(eGamePlayerEventType.UnionAbilityRiseRank.getValue(), new UnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_REMOVE, BigInteger.ZERO, unionUid));
            player.notifyListener(eGamePlayerEventType.UnionEarnSpeedRiseRank.getValue(), new UnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_REMOVE, BigInteger.ZERO, unionUid));

            UnionRechargeUserArgs args = new UnionRechargeUserArgs(UnionRechargeUserArgs.VALUE_IN, 0, unionUid, userId);
            args.setSetValue(BigInteger.valueOf(0));
            player.notifyListener(eGamePlayerEventType.UnionRechargeUser.getValue(), args);
        } else {
            List<ActivityInfo> activityInfoList = NormalActivityMgr.getUnionRiseRankActivityList();
            for (ActivityInfo activityInfo : activityInfoList) {
                UnionActivityMgr.changeUnionActivityValue(activityInfo.getActivityId(), unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_REMOVE);
            }
        }
    }

    private static void removeMemberCommonActivityConditionData(long userId, String unionUid) {
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        List<ActivityInfo> activityInfoList = CommonActivityMgr.getInTimeCommonActivityInfoList(eActivityType.CommonActivity.getValue());
        if (null != player) {
            for (ActivityInfo activityInfo : activityInfoList) {
                player.notifyListener(eGamePlayerEventType.CommonActivityScoreRiseRank.getValue(),
                        new CommonActivityUnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_REMOVE, BigInteger.ZERO, unionUid, activityInfo.getChildType()));
            }
        } else {
            for (ActivityInfo activityInfo : activityInfoList) {
                CommonActivityRankMgr.changeUnionActivityValue(activityInfo.getActivityId(), unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_REMOVE);
            }
        }
    }

    //移除商会成员盐场活动排行榜移除玩家
    private static void removeMemberOreWarActivityConditionData(long userId, String unionUid) {
        GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
        if (null != player) {
            player.notifyListener(eGamePlayerEventType.OreWarAcCoinOutPut.getValue(), new UnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_REMOVE, BigInteger.ZERO, unionUid));
            player.notifyListener(eGamePlayerEventType.CrossOreWarAcCoinOutPut.getValue(), new UnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_REMOVE, BigInteger.ZERO, unionUid));
        } else {
            List<ActivityInfo> activityInfoList = OreWarActivityMgr.getOpenActivityInfoList();
            for (ActivityInfo activityInfo : activityInfoList) {
                OreWarActivityRankMgr.changeUnionActivityValue(activityInfo.getActivityId(), unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_REMOVE);
            }
        }
    }

    private static void removeMemberDaFuWengActivityConditionData(long userId, String unionUid) {
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.DaFuWeng.getValue());
        if (activityInfoList != null) {
            for (ActivityInfo activityInfo : activityInfoList) {
                List<Integer> typeList = NormalActivityMgr.getActivityConditionUnionRankType(activityInfo.getActivityId());
                for (Integer type : typeList) {
                    GeneralActivityRankMgr.changeUnionActivityValue(activityInfo.getActivityId(), type, unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_REMOVE);
                }
            }
        }
    }

    private static void removeMemberBowArtActivityConditionData(long userId, String unionUid){
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.BowArtActivity.getValue());
        if (activityInfoList != null) {
            for (ActivityInfo activityInfo : activityInfoList) {
                List<Integer> typeList = NormalActivityMgr.getActivityConditionUnionRankType(activityInfo.getActivityId());
                for (Integer type : typeList) {
                    GeneralActivityRankMgr.changeUnionActivityValue(activityInfo.getActivityId(), type, unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_REMOVE);
                }
            }
        }
    }

    private static void removeMemberGhostActivityConditionData(long userId, String unionUid) {
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.GhostActivity.getValue());
        if (activityInfoList != null) {
            for (ActivityInfo activityInfo : activityInfoList) {
                List<Integer> typeList = NormalActivityMgr.getActivityConditionUnionRankType(activityInfo.getActivityId());
                for (Integer type : typeList) {
                    GeneralActivityRankMgr.changeUnionActivityValue(activityInfo.getActivityId(), type, unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_REMOVE);
                }
            }
        }
    }

    private static void removeMemberDragonBoatRaceActivityConditionData(long userId, String unionUid){
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.DragonBoatRaceActivity.getValue());
        if (activityInfoList != null) {
            for (ActivityInfo activityInfo : activityInfoList) {
                List<Integer> typeList = NormalActivityMgr.getActivityConditionUnionRankType(activityInfo.getActivityId());
                for (Integer type : typeList) {
                    GeneralActivityRankMgr.changeUnionActivityValue(activityInfo.getActivityId(), type, unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_REMOVE);
                }
            }
        }
    }

    private static void removeMemberRelationActivityConditionData(long userId, String unionUid) {
        //为什么上面的没用这种方式
        List<ActivityInfo> activityInfoList = RelationActivityMgr.getOpenActivityInfoList();
        for (ActivityInfo activityInfo : activityInfoList) {
            if (activityInfo.getType() == eActivityType.CookingBoyActivity.getValue() && activityInfo.getChildType() == eActivityCookBoyType.Process1.getChildType()) {
                if (ActivityMgr.activityInTime(activityInfo)) {
                    RelationActivityMgr.conditionNotify(activityInfo.getActivityId(),userId, eGamePlayerEventType.CookBoy1IntegralUserCrossRank.getValue(),
                            new RelationActivityUnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_REMOVE, BigInteger.ZERO, unionUid, activityInfo.getActivityId()));
                }
            }
        }
    }

    private static void removeMemberSeacraftPirateRiotChange(long userId, String unionUid) {
        SeacraftPirateRiotConfig config = SeacraftPirateRiotMgr.getPirateRiotConfig();
        if (SeacraftPirateRiotMgr.inUnionRankChangeTime(config)) {
            ActivityInfo activityInfo = config.getActivityInfo();
            GeneralActivityRankMgr.changeUnionActivityValue(activityInfo.getActivityId(), eGamePlayerEventType.SeacraftPirateRiotUnionScoreRank.getValue(), unionUid, userId, null, UnionRiseRankArgs.NOTIFY_TYPE_REMOVE);
        }
    }
}
